/**
 * @PROJECT 
 * @DATE 2018年7月22日 下午7:44:27
 * @AUTHOR LUYU
 */
package com.kool.evowkf.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.core.base.BaseBean;
import com.kool.core.base.IBaseAction;
import com.kool.core.exception.AppException;
import com.kool.core.util.BeanUtils;
import com.kool.core.util.GlobalDefineUtils;
import com.kool.evowkf.bean.SyWkfRecordBean;
import com.kool.evowkf.bean.io.approve.CMQRYWKFRCDXBean;
import com.kool.evowkf.bean.io.approve.CMQRYWKFRCDZBean;
import com.kool.evowkf.service.WkfService;
import com.kool.system.bean.io.SYPAGEINFOYBean;
import com.kool.system.bean.vo.PageResult;

/**
 * @DESCRIBE 分页查询流程执行记录
 * @AUTHOR LUYU
 * @DATE 2018年7月22日 下午7:44:27
 *
 */
@Controller
public class QueryRecordPageAction implements IBaseAction {
	@Autowired
	private WkfService service;

	@Override
	@ResponseBody
	@RequestMapping(value = "/comm/wkf/queryWkfRecordListPage", method = RequestMethod.POST)
	public JSONObject doAction(@RequestBody String sInput) throws AppException {
		JSONObject pkgIn = JSONObject.parseObject(sInput);
		JSONArray secIn = pkgIn.getJSONArray("CMQRYWKFRCDX");
		SyWkfRecordBean recInfo = null;
		if (secIn != null && secIn.size() > 0) {
			JSONObject propIn = secIn.getJSONObject(0);
			CMQRYWKFRCDXBean ioIn = JSON.toJavaObject(propIn, CMQRYWKFRCDXBean.class);
			recInfo = (SyWkfRecordBean) BeanUtils.io2Bean(ioIn, SyWkfRecordBean.class, "src");
		} else {
			recInfo = new SyWkfRecordBean();
		}

		JSONArray sectionInPageInfo = pkgIn.getJSONArray("SYPAGEINFOY");
		JSONObject propInPageInfo = sectionInPageInfo.getJSONObject(0);
		SYPAGEINFOYBean pageInfo = JSONObject.toJavaObject(propInPageInfo, SYPAGEINFOYBean.class);

		PageResult pageResult = service.queryRecordListPage(recInfo, pageInfo);

		JSONObject pkgOut = new JSONObject();
		JSONArray secOut = new JSONArray();
		for (BaseBean record : pageResult.getListBusinessValue()) {
			CMQRYWKFRCDZBean ioOut = (CMQRYWKFRCDZBean) BeanUtils.bean2Io(record, CMQRYWKFRCDZBean.class);
			JSONObject propOut = (JSONObject) JSON.toJSON(ioOut);
			GlobalDefineUtils.addTransformation(propOut, "businessType", "SRC_BUSINESS_TYPE");
			GlobalDefineUtils.addTransformation(propOut, "state", "SRC_STATE");
			secOut.add(propOut);
		}
		pkgOut.put("CMQRYWKFRCDZ", secOut);

		JSONArray sectionOutPageInfo = new JSONArray();
		pageInfo = pageResult.getPageInfo();
		JSONObject propOutPageInfo = JSONObject.parseObject(JSONObject.toJSONString(pageInfo));
		sectionOutPageInfo.add(propOutPageInfo);
		pkgOut.put("SYPAGEINFOY", sectionOutPageInfo);
		return pkgOut;
	}

}
